<?php
error_reporting(E_ALL ^ E_NOTICE);

if (!function_exists('D')) {
  /**
   * 实例化模型
   * @param string $name 路径分割用.隔开
   * @param string $prefix 模型前缀
   * @return object|\think\App
   */
  function D($name = '',$prefix="app\\common\\model\\") {
    $name   = str_replace(".", "\\", $name);
    $model  = $prefix . $name . 'Model';
    return app($model);
  }
}
if (!function_exists('html_out')) {
  /**
   * 还原html
   * @param $html
   * @return string
   */
  function html_out($html) {
    return helper\Utils::htmlOut($html);
  }
}
if (!function_exists('format_number')) {
  /**
   * 格式化价格
   * @param $price
   * @param int $digits //保留几位小数，默认取配置项
   * @return false|float
   */
  function format_number($price, $digits = 2) {
    return sprintf("%.{$digits}f", $price);
  }
}
if (!function_exists('get_attachment_json')) {
  /**
   * 获取json格式附件资源
   * @param $urls
   * @param int $type
   * @return array|mixed|string
   * @throws \Exception
   */
  function get_attachment_json($urls, $type = 1) {
    switch ($type) {
      case 1: //单附件
        $list = D('Attachment')->where('file_url', $urls)->select()->toJson();
        break;
      case 2: //逗号隔开多附件
        $list = D('Attachment')->where('file_url', 'in', $urls)->select()->toJson();
        break;
      default:
        $list = $urls;
        break;
    }
    return $list;
  }
}

if (!function_exists('format_tree_select')) {
  /**
   * 树型下拉不应该选中数据
   * @param $list
   * @param string $path
   * @param string $key
   * @return mixed
   */
  function format_tree_select($list, string $path, $key = 'id')
  {
    foreach ($list as $k => $v) {
      $v['disabled'] = in_array($v[$key], explode(',', $path)) ? 'disabled' : '';
      $list[$k] = $v;
    }
    return $list;
  }
}
if (!function_exists('get_dict_value')) {
  /**
   * 获取字典数据
   * @param $classCode
   * @param $dictCode
   * @param bool $refresh
   * @return string
   * @throws \Exception
   */
  function get_dict_value($classCode, $dictCode, $refresh = false)
  {
    if ($refresh == true || !cache("?dict")) {
      $dictList = D('Dict')::order('show_order asc')->select()->toArray();
      $cache    = [];
      foreach ($dictList as $v) {
        $cache[$v['class_code'] . '.' . $v['dict_code']] = $v;
      }
      cache('dict', $cache);
    }
    $dict = cache('dict');
    return isset($dict[$classCode . '.' . $dictCode]) ? $dict[$classCode . '.' . $dictCode]['dict_value'] : '-';
  }
}
if (!function_exists('get_val')) {
  /**
   * 获取对应值
   * @param $arr
   * @param $key
   * @param string $default
   * @return string
   */
  function get_val($arr, $key, $default = '-')
  {
    return isset($arr[$key]) ? $arr[$key] : $default;
  }
}
if (!function_exists('md5_16')) {
  /**
   * 获取16位md5值
   * @param $val
   * @return string
   */
  function md5_16($val)
  {
    return substr(md5($val), 8, 16);
  }
}

if (!function_exists('patch')) {
  /**
   * 获取patch内容
   * 获取patch内容
   * @param $patchSn
   * @return string
   * @throws \Exception
   */
  function patch($patchSn) {
    $info = D('Patch')->where('patch_sn', $patchSn)->find();
    $content = '';
    if(!empty($info)){
      switch ($info['patch_type']){
        case '1': //单图碎片
          $content = $info['patch_content'];
          break;
        case '2': //多图碎片
        case '5': //多附件碎片
        case '6': //项目列表碎片
          $content = json_decode($info['patch_content'], true);
          break;
        case '3': //代码碎片
        case '4': //富文本碎片
          $content = \helper\Utils::htmlOut($info['patch_content']);
          break;
      }
    }
    return $content;
  }
}
if (!function_exists('web_config')) {
  /**
   * 获取配置值
   * @param $key
   * @return mixed
   */
  function web_config($key) {
    if(cache("?web_config")){
      $config = cache('web_config');
    }else{
      $config = D('Config')->refreshCache();
    }
    return isset($config[$key]) ? $config[$key] : '';
  }
}
if (!function_exists('dict')) {
  /**
   * 获取字典数据
   * @param $classCode
   * @param $dictCode
   * @param bool $refresh
   * @return string
   * @throws \Exception
   */
  function dict($classCode, $dictCode, $refresh = false) {
    if ($refresh == true || !cache("?dict")) {
      $dictList = \app\common\model\DictModel::order('show_order asc')->select()->toArray();
      $cache    = [];
      foreach ($dictList as $v) {
        $cache[$v['class_code'] . '.' . $v['dict_code']] = $v;
      }
      cache('dict', $cache);
    }
    $dict = cache('dict');
    return isset($dict[$classCode . '.' . $dictCode]) ? $dict[$classCode . '.' . $dictCode]['dict_value'] : '';
  }
}
